package cc.comac.mom.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cc.comac.mom.api.QualityTestContentService;
import cc.comac.mom.bean.QualityAttachmentGroup;
import cc.comac.mom.bean.QualityTestContent;
import cc.comac.mom.bean.base.LimitPageBean;
import cc.comac.mom.datahandlers.entity.QualityAttachmentGroupDataHandler;
import cc.comac.mom.datahandlers.entity.QualityTestContentDataHandler;
import cc.comac.mom.entity.QualityTestContentEntity;
import cc.comac.mom.redis.IdFactory;
import cc.comac.mom.util.ResultBean;

@Service
@RestController
public class QualityTestContentServiceImpl implements QualityTestContentService {

	@Autowired
	private QualityTestContentDataHandler qualityTestContentDataHandler;

	@Resource
	private IdFactory idFactory;

	@Autowired
	private QualityAttachmentGroupDataHandler qualityAttachmentGroupDataHandler;

	@Override
	public ResultBean insert(@RequestBody QualityTestContent obj) {


		ResultBean resultBean = new ResultBean(true);

		if (obj != null && obj.getInnerData() != null) {
			List<QualityAttachmentGroup> listAttachmentGroup = obj.getListQualityAttachmentGroup();
			if (listAttachmentGroup != null && listAttachmentGroup.size() > 0) {
				long id = idFactory.generateId();
				obj.getInnerData().setAttachmentGroupId(id);
				for (QualityAttachmentGroup item : listAttachmentGroup) {
					item.getInnerData().setGroupId(id);
				}
				ResultBean resultBean2 = qualityAttachmentGroupDataHandler.insertBatch(listAttachmentGroup);
				if (!resultBean2.success) {
					throw new RuntimeException("保存附件组信息失败");
				}
			}
		}
		ResultBean resultBean2 = qualityTestContentDataHandler.insert(obj);
		if (!resultBean2.success) {
			throw new RuntimeException("保存失败");
		}

		return resultBean;
	}

	@Override
	public ResultBean selectByCondition(@RequestBody QualityTestContent obj) {
		ResultBean resultBean = new ResultBean(true);
		LimitPageBean<QualityTestContentEntity> selectByCondition = qualityTestContentDataHandler
				.selectByCondition(obj);
		resultBean.setObject(selectByCondition);
		return resultBean;
	}

	@Override
	public ResultBean getByTestNo(@RequestParam String testNo) {
		ResultBean resultBean = new ResultBean(true);
		List<QualityTestContent> listQualityTestContent = qualityTestContentDataHandler.getByTestNo(testNo);
		if (listQualityTestContent != null && listQualityTestContent.size() > 0) {
			for (QualityTestContent item : listQualityTestContent) {
				long attachmentGroupId = item.getInnerData().getAttachmentGroupId();
				if (attachmentGroupId != 0) {
					List<QualityAttachmentGroup> listQualityAttachmentGroup = qualityAttachmentGroupDataHandler
							.getByAttachmentGroupId(attachmentGroupId);
					item.setListQualityAttachmentGroup(listQualityAttachmentGroup);
				}
			}
		}
		resultBean.setObject(listQualityTestContent);
		return resultBean;
	}

}
